<?php
/**
 * Semester Teacher Setup  Controller
 * Semester Teacher  Setup   Management
 *
 * semester entry
 *
 * @version $Id: TeachersetupController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package semester
 * @subpackage Controller
 */
class semester_TeachersetupController extends Zend_Controller_Action
{
    function indexAction()
    {
        //學年學期
        if (!$this->_request->getParam('year_semester'))
        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        else
        $this->view->year_semester = $this->_request->getParam('year_semester');
        //年級
        if (!$this->view->sel_grade = $this->_request->getParam('sel_grade'))
        $this->view->sel_grade = $this->view->lowYear;  // 預設最低學年



        // 課程代碼
        //	$subject = new semester_subject();
        //	$this->view->subject_key = Zend_Json::encode($subject->getIndexKey());

        //TODO 使用代碼輸入 班級 課程 例 101 01 04 一甲 國語 四
    }


    /**
     * 設定資料
     * @return unknown_type
     */
    function setAction()
    {

        $yearSemester = $this->_request->getParam('year_semester');
        if ($yearSemester) {
            $yearSemesterArr = explode("-",$yearSemester);
            $year = (int)$yearSemesterArr[0];
            $semester = (int)$yearSemesterArr[1];
        }
        $number = (int)$this->_request->getParam('number',0);
        $classId = explode("-",$this->_request->getParam('sel_grade'));
        $grade = (int)$classId[0];
        $classSn = (int)$classId[1];
        $subjectSn = (int)$this->_request->getParam('sel_course');
        $selTeacher = (int)$this->_request->getParam('sel_teacher');
        if ($number >0 and $subjectSn >0 and $selTeacher >0) {
            $semesterTeacherCourse = new  Semester_Model_SemesterTeacherCourse();
            $row = $semesterTeacherCourse->getTable()->createQuery('a')
            ->where("year=? AND semester=? AND grade=?  AND class_sn=? AND subject_sn=? AND teacher_sn=?",
            array($year, $semester, $grade,  $classSn, $subjectSn, $selTeacher) )->fetchOne();
            if ($row)
            $semesterTeacherCourse->assignIdentifier($row['sn']);
            $semesterTeacherCourse->year = $year;
            $semesterTeacherCourse->semester = $semester;
            $semesterTeacherCourse->grade = $grade;
            $semesterTeacherCourse->class_sn = $classSn;
            $semesterTeacherCourse->subject_sn = $subjectSn;
            $semesterTeacherCourse->number = $number;
            $semesterTeacherCourse->teacher_sn = $selTeacher;
            $semesterTeacherCourse->save();
            $this->_helper->json('ok');
        }
        else {
            $this->_helper->json('error');
        }
    }

    /**
     * 刪除設定
     * @return unknown_type
     */
    function deleteAction()
    {
        $sn = (int)$this->_request->getParam('id');
       // 查詢 是否有排課資料
       $q = Doctrine_Query::create()
       ->select('COUNT(*) AS cc')
       ->from('Semester_Model_SemesterTimetable a')
        ->leftJoin('a.Semester_Model_SemesterTeacherCourse b')
        ->leftJoin('a.Semester_Model_SemesterSection  c')
       ->where('b.year=c.year AND b.semester=c.semester AND b.grade=c.grade AND a.class_sn=b.class_sn')
       ->andwhere('b.sn=?',$sn);
//        echo $q->getsqlQuery();exit;
       if ($q->fetchOne()->cc == 0 ) {
        $semesterTeacherCourse = new Semester_Model_SemesterTeacherCourse();
        $semesterTeacherCourse->assignIdentifier($sn);
        $semesterTeacherCourse->delete();
        $this->_helper->json('ok');
       }
       else
        $this->_helper->json(0);
    }

    /**
     * 取得教師本學期配課列表
     * @return unknown_type
     */
    function teachercourseAction()
    {
        $this->getHelper ( 'layout' )->disableLayout ();
        $yearSemester = $this->_request->getParam('year_semester');
        // 班級
        $semester = new Semester_Model_SemesterClass;
        $this->view->class_arr = $semester->getClassNames($yearSemester);

        $teacherSn = (int)$this->_request->getParam('teacher_sn');
        $model = new Semester_Model_SemesterTeacherCourse();
        $this->view->data = $model->getTeacherCourse($yearSemester,null,$teacherSn);
    }

    /**
     * 取本學期所有教師配課數
     * @return unknown_type
     */
    function teachercountAction()
    {
        $this->getHelper ( 'layout' )->disableLayout ();
        $yearSemester = $this->_request->getParam('year_semester');
        $model = new Semester_Model_SemesterTeacherCourse();
        $this->view->data = $model->fetchTeacherCourseCount($yearSemester);
    }

    /**
     * 列出本學期班級配課情形
     * @return unknown_type
     */
    function classcountAction()
    {
        $this->getHelper ( 'layout' )->disableLayout ();
        $yearSemester = $this->_request->getParam('year_semester');
        $classId = explode("-",$this->_request->getParam('class_id'));
        $grade = (int) $classId[0];
        $classSn = (int) $classId[1];
        $model = new Semester_Model_SemesterTeacherCourse;
        $this->view->data = $model->fetchClassCourseCount($yearSemester,null,$grade, $classSn);
    }

    /**
     * 科目列表
     * @return json format
     */
    function courselistAction()
    {
    	 $this->getHelper ( 'layout' )->disableLayout ();
        $yearSemester = $this->_request->getParam('year_semester');
        $classId = explode("-",$this->_request->getParam('class_id'));
        $grade = (int)$classId[0];
        $classSn = (int)$classId[1];
        $model = new Semester_Model_SemesterTeacherCourse();
       $res = $model->fetchCourseList($yearSemester,null,$grade, $classSn);


        $arr = array();
        foreach ($res as $row){
        	 $arr[$row['subject_sn']] =  $row['name'];
           }
        $this->view->data  = $arr;
          $this->_helper->json($arr);
    }
}
